const express=require('express')
const router= express.Router();
const db=require('../../utils/db')

router.get('/getbookorder', function(req, res, next) {
    let mysql = `SELECT * FROM booking_order join customer on booking_order.book_username = customer.customer_id join table_area on booking_order.book_table_size = table_area.table_id`
    db(mysql, null, function(err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询订单信息失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询订单信息成功',
            data: data
        })
    })
});
router.get('/searchbookid',function(req,res){
    let mysql=`SELECT * FROM booking_order 
    join customer on booking_order.book_username = customer.customer_id 
    join table_area on booking_order.book_table_size = table_area.table_id 
    WHERE book_num LIKE ? GROUP BY book_num`
    db(mysql,['%'+req.query.book_num+'%'],function(err,data){
        if(err){
            return res.json({
                code: 500,
                msg: '模糊查询预约订单失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '模糊查询预约订单成功',
            data: data
        })
    })
})

router.post('/delbookid',function(req,res){  
    let mysql='DELETE FROM booking_order WHERE book_id=?';
    db(mysql,[req.body.book_id],(err,data)=>{
        if(err){
            return res.json({
                code: 500,
                msg: '删除订单失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '删除订单成功',
            data: data
        })
    })
})

// 修改菜品状态
router.post('/editbookorder',function(req,res){
    let mysql=`UPDATE booking_order SET book_peoplenum='${req.body.book_peoplenum}',book_wait_time='${req.body.book_wait_time}',book_eat_time='${req.body.book_eat_time}',book_status='${req.body.book_status}' WHERE book_num='${req.body.book_num}'`
    db(mysql,null,(err,data)=>{
        if(err){
            return res.json({
                code: 500,
                msg: '修改菜品状态失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '修改菜品状态成功',
            data: data
        })
    })
})
module.exports = router